package cn.kgc.web;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class BaseServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String methodName = request.getParameter("methodName");

        try {
            Class clazz = this.getClass();
            Method method = clazz.getMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
            String path = (String) method.invoke(this,request,response);
            if(path!=null){
                if(path.contains(request.getContextPath())){
                    response.sendRedirect(path);
                }else{
                    request.getRequestDispatcher(path).forward(request,response);
                }
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
